/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slicktest.GameStates;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import slicktest.constants.MapConstants;
import slicktest.constants.MenuConstants;
import slicktest.entities.ImmobileEntity.Terrain;
import slicktest.entities.ImmobileEntity.Tower;
import slicktest.entities.InputEntity.TowerBuildMenu;
import slicktest.entities.InputEntity.TowerContextMenu;
import slicktest.entities.maps.GameMap;
import slicktest.factories.GameEntityFactory;

/**
 *
 * @author enrightt
 */
public class GameplayState extends BasicGameState {

    private int id = -1;
    private GameMap map;
    private TowerContextMenu towerMenu;
    private TowerBuildMenu buildMenu;
    private GameEntityFactory factory;

    public GameplayState(int id) {
        this.id = id;
    }

    @Override
    public int getID() {
        return id;
    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        //TODO: make all of the things that are doing new into factory calls
        factory = GameEntityFactory.getInstance();
        this.map = new GameMap(MapConstants.MAP_ROWS,
                MapConstants.MAP_COLS,
                MapConstants.TILE_WIDTH,
                MapConstants.TILE_HEIGHT,
                (MapConstants.SCREEN_WIDTH / 2),
                (MapConstants.SCREEN_HEIGHT / 2),
                MapConstants.BUFFER);       

        towerMenu = new TowerContextMenu(MenuConstants.FILL,
                Color.yellow,
                Color.red,
                map,
                MenuConstants.CONTEXT_MENU_WIDTH,
                MenuConstants.CONTEXT_MENU_HEIGHT);
        gc.getInput().addListener(towerMenu);

        buildMenu = new TowerBuildMenu(MapConstants.SCREEN_WIDTH,
                MenuConstants.BUILD_MENU_HEIGHT,
                MapConstants.SCREEN_WIDTH,
                MapConstants.SCREEN_HEIGHT,
                MenuConstants.FILL,
                Color.yellow,
                Color.red);


        boolean odd = false;
        for (int i = 0; i < MapConstants.MAP_ROWS; i++) {
            for (int j = 0; j < MapConstants.MAP_COLS; j++) {
                Terrain t;
                if (!odd) {
                    t = factory.createTerrain("grass");
                } else {
                    t = factory.createTerrain("rocks");
                }
                map.addTerrain(t, i, j);
                odd = !odd;
            }

            odd = !odd;
        }

        Tower mageTower = factory.createTower("mage tower");
        map.addTower(mageTower, 0, 0);
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        map.render(gc, g);
        towerMenu.render(gc, g);
        buildMenu.render(gc, g);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
        map.update(gc, delta);
        towerMenu.update(gc, delta);
        buildMenu.update(gc, delta);

    }
}
